<?php
namespace frontend\models;

use yii\base\Model;
use frontend\models\User;

/**
 * Signup form
 */
class MiniSignupForm extends Model
{
    public $plate_number;
    public $mobile;
    public $code;
    public $password;
    public $password2;


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['plate_number','mobile','code','password','password2'], 'trim'],
            [['plate_number','mobile','code','password'], 'required'],
            ['mobile', 'unique', 'targetClass' => 'frontend\models\User', 'message' => '手机号已经被注册.'],
            [['plate_number','code'], 'string', 'min' => 2, 'max' => 255],
            [['password','password2'],'string','min' => 6, 'max' => 12,'message' => '密码长度为6-12位.'],
            ['password2', 'compare','compareAttribute'=>'password'],
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }

        $user = new User();
        $user->username = $this->mobile;
        $user->mobile = $this->mobile;
        $user->plate_number = $this->plate_number;
        $user->setPassword($this->password);
        $user->generateAuthKey();

        return $user->save() ? $user : null;
    }
}
